package com.lee.mao.database.utils;

import android.util.Log;

import com.lee.mao.base.Mapplication;
import com.lee.mao.bean.MoneyTable;
import com.lee.mao.database.dao.MoneyTableDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class MoneyTableUtil {
    private static MoneyTableUtil instance;

    private static final String TAG = "MoneyTableUtil";
    private static Mapplication mapplication;

    public static MoneyTableUtil getInstance(Mapplication app) {
        mapplication = app;
        if (instance == null) {
            synchronized (MoneyTableUtil.class) {
                if (instance == null) {
                    instance = new MoneyTableUtil();
                }
            }
        }
        return instance;
    }

    private MoneyTableUtil() {
    }

    private MoneyTableDao getTableDao() {

        if (mapplication != null)
            return mapplication.getDaoSession().getMoneyTableDao();
        else
            return null;
    }

    /**
     * eq()："equal ('=?')" 等于；
     * notEq() ："not equal ('<>?')" 不等于；
     * like()：" LIKE ?" 值等于；
     * between()：" BETWEEN ? AND ?" 取中间范围；
     * in()：" IN ("  in命令;
     * notIn()：" NOT IN (" not in 命令;
     * gt()：">?"  大于;
     * lt()："<? "  小于;
     * ge()：">=?"  大于等于;
     * le()："<=? "  小于等于;
     * isNull()：" IS NULL" 为空;
     * isNotNull()：" IS NOT NULL" 不为空;
     */

    public List<MoneyTable> getChargeByTime(long startTime, long endTime, int type) {

        MoneyTableDao tableDao = getTableDao();

        Log.d(TAG, "startTime:" + startTime + ",endTime:" + endTime);

        if (tableDao != null) {

            QueryBuilder<MoneyTable> queryBuilder = tableDao.queryBuilder();
            return queryBuilder.where(MoneyTableDao.Properties.ChargeTime.ge(startTime)
                    , MoneyTableDao.Properties.ChargeTime.lt(endTime), MoneyTableDao.Properties.Type.eq(type)).orderDesc(MoneyTableDao.Properties.ChargeTime).list();

        }
        return null;

    }


    public void queryAll() {

        MoneyTableDao tableDao = getTableDao();
        if (tableDao != null) {

            List<MoneyTable> MoneyTables = tableDao.loadAll();

            Log.d(TAG, "查询出的个数:" + MoneyTables.size());
            for (int i = 0; i < MoneyTables.size(); i++) {
                Log.d(TAG, MoneyTables.get(i).toString());
            }

        }
    }


    public void addCharge(MoneyTable MoneyTable) throws Exception {

        MoneyTableDao tableDao = getTableDao();

        if (tableDao != null)
            tableDao.insert(MoneyTable);
        else
            throw new Exception("插入失败,tableDao为null");
    }


    public void deleteCharge(long id) throws Exception {

        MoneyTableDao tableDao = getTableDao();

        if (tableDao != null)
            tableDao.deleteByKey(id);
        else
            throw new Exception("删除失败,tableDao为null");

    }


    public void updateCharge(MoneyTable MoneyTable) throws Exception {
        MoneyTableDao tableDao = getTableDao();

        if (tableDao != null)
            tableDao.update(MoneyTable);
        else
            throw new Exception("更新失败,tableDao为null");
    }

}